package class61To70;

import java.io.*;
import java.util.Arrays;


// https://www.luogu.com.cn/problem/P3385
public class BellmanFordAndSPFA {
    public static int MAX_N = 2001;

    public static int MAX_M = 6001;

    public static int[] head = new int[MAX_N];

    public static int[] next = new int[MAX_M];

    public static int[] to = new int[MAX_M];

    public static int[] weight = new int[MAX_M];

    public static int cnt;

    public static int MAX_Q = 4000001;

    public static int[] queue = new int[MAX_Q];
    public static int l, r;

    public static boolean[] enter = new boolean[MAX_N];

    public static int[] updateCnt = new int[MAX_N];

    public static int[] distance = new int[MAX_N];

    public static int n, m, t, u, v, w;

    public static void build(int n) {
        cnt = 1;
        l = r = 0;
        Arrays.fill(head, 0, n + 1, 0);
        Arrays.fill(distance, 0, n + 1, Integer.MAX_VALUE);
        Arrays.fill(updateCnt, 0, n + 1, 0);
        Arrays.fill(enter, 0, n + 1, false);

    }

    public static void addEdge(int u, int v, int w) {
        next[cnt] = head[u];
        to[cnt] = v;
        weight[cnt] = w;
        head[u] = cnt++;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        in.nextToken();
        t = (int) in.nval;
        for (int i = 0; i < t; i++) {
            in.nextToken();
            n = (int) in.nval;
            build(n);
            in.nextToken();
            m = (int) in.nval;
            for (int j = 0; j < m; j++) {
                in.nextToken();
                u = (int) in.nval;
                in.nextToken();
                v = (int) in.nval;
                in.nextToken();
                w = (int) in.nval;
                if (w >= 0) {
                    addEdge(u, v, w);
                    addEdge(v, u, w);
                } else {
                    addEdge(u, v, w);
                }
            }
            out.println(spfa() == true ? "YES" : "NO");
        }
        out.flush();
        out.close();
        br.close();
    }

    private static boolean spfa() {
        distance[1] = 0;
        updateCnt[1]++;
        queue[r++] = 1;
        enter[1] = true;
        while (l < r) {
            int cur = queue[l++];
            enter[cur] = false;
            for (int ei = head[cur]; ei > 0; ei = next[ei]) {
                int v = to[ei];
                int w = weight[ei];
                if (distance[v] > distance[cur] + w) {
                    distance[v] = distance[cur] + w;
                    if (!enter[v]) {
                        if (++updateCnt[v] > n - 1) {
                            return true;
                        }
                        queue[r++] = v;
                        enter[v] = true;
                    }
                }
            }
        }
        return false;
    }
}
